﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using TreaShares.Backoffice.DataLayer;

namespace TreaShares.Backoffice.Controlers
{

    [DataObjectAttribute(true)]
    public class CounterpartyManager
    {
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<CounterParty> Select(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                return db.CounterParties.Where(c => c.ClientId == cId && c.IsActive == true).Select(c => c).ToList();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<CounterParty> SelectAll(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                return db.CounterParties.Where(c => c.ClientId == cId ).Select(c => c).ToList();
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void Delete(int Id)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.CounterParties.DeleteOnSubmit(db.CounterParties.Where(c => c.Id == Id).Single());
                db.SubmitChanges();
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public void Update(int Id, string shortName, string longName, bool isActive, string ClientId, bool isInternal, bool optionPlan)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                var counterparty = db.CounterParties.Where(e => e.Id == Id).Single();
                counterparty.ShortName = shortName;
                counterparty.LongName = longName;
                counterparty.IsActive = isActive;
                counterparty.ClientId = new Guid(ClientId);
                counterparty.IsInternal = isInternal;
                counterparty.OptionPlan = optionPlan;
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public void InsertCounterParty(string shortName, string longName, bool isActive, string ClientId, bool isInternal, bool optionPlan)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                db.CounterParties.InsertOnSubmit(new CounterParty() { ShortName = shortName, LongName = longName, IsActive = isActive,
                                                                      ClientId = cId,
                                                                      IsInternal = isInternal,
                                                                      OptionPlan = optionPlan
                });
                db.SubmitChanges();
            }
        }
    }
}